﻿using GylCSharpUtils.Common.Abstract;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;

namespace GylCSharpUtils.Common.Utils {
    public class MSBinSerializer : ASerializer {


        public override object Deserialize(byte[] data) {
            using(MemoryStream ms = new MemoryStream(data)) {
                BinaryFormatter bf = new BinaryFormatter();
                try {
                    return bf.Deserialize(ms);
                } catch(Exception ex) {
                    Console.WriteLine(ex.ToString());
                }
                return bf.Deserialize(ms);
            }
        }


        public override byte[] Serialize(object obj) {
            using(MemoryStream ms = new MemoryStream()) {
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(ms, obj);
                return ms.ToArray();
            }
        }
    }
}
